package de.revdev.streckenliste.managedbeans.converter;

import de.revdev.streckenliste.dao.WildtypDAO;
import de.revdev.streckenliste.entities.Wildtyp;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

/**
 *
 * @author f.lindermaier
 */
@ManagedBean(name = "wildtypConverter")
public class WildtypConverter implements Converter {

    @Inject
    private WildtypDAO wtdao;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        Long id = new Long(value);
        Wildtyp wt = wtdao.find(id.longValue());
        return wt;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value.toString().equals("")) {
            return "";
        }
        Long id = ((Wildtyp) value).getId();
        return id.toString();
//        return ((Wildtyp) value).toString();
    }
}
